package com.example.list.swiperefresh;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.list.R;
import com.example.list.recyleview.MyAdapter;
import com.example.list.recyleview.TV_MSG;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


import java.util.ArrayList;
import java.util.List;

public class SwipeRefreshActivity extends AppCompatActivity {

    private SmartRefreshLayout refreshLayout;
    private RecyclerView sa_rl;
    private int lastVisibleItem;
    private List<TV_MSG> arrayList = new ArrayList<>();

    boolean isUpScroll = false; //是否正在上滑标记

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_refresh);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.sa_refresh);
        sa_rl = (RecyclerView) findViewById(R.id.sa_rl);

        arrayList.add(new TV_MSG("1", Color.RED));
        arrayList.add(new TV_MSG("2", Color.parseColor("#F4B50D")));
        arrayList.add(new TV_MSG("3", Color.YELLOW));
        arrayList.add(new TV_MSG("4", Color.GREEN));
        arrayList.add(new TV_MSG("5", Color.parseColor("#FFFF00")));
        arrayList.add(new TV_MSG("6", Color.BLUE));
        arrayList.add(new TV_MSG("7", Color.parseColor("#9F009A")));
        arrayList.add(new TV_MSG("1", Color.RED));
        arrayList.add(new TV_MSG("2", Color.parseColor("#F4B50D")));
        arrayList.add(new TV_MSG("3", Color.YELLOW));
        arrayList.add(new TV_MSG("4", Color.GREEN));
        arrayList.add(new TV_MSG("5", Color.parseColor("#FFFF00")));
        arrayList.add(new TV_MSG("6", Color.BLUE));
        arrayList.add(new TV_MSG("7", Color.parseColor("#9F009A")));
        arrayList.add(new TV_MSG("7", Color.parseColor("#9F009A")));
//单一布局
        final MyAdapter adapter = new MyAdapter(this, arrayList);

        final GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        sa_rl.setLayoutManager(layoutManager);
        sa_rl.setAdapter(adapter);


        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(1000/*,false*/);//传入false表示刷新失败
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        arrayList.clear();
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
//                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        adapter.notifyDataSetChanged();
                    }
                }, 1000);

            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        arrayList.add(new TV_MSG("refresh", Color.RED));
                        adapter.notifyDataSetChanged();
                    }
                }, 1000);
            }
        });



    }

}